#!/usr/bin/env python
# -*- encoding: utf-8 -*-
import time
from bs4 import BeautifulSoup
import requests
import base64
import os
from qcloud_cos import CosConfig
from qcloud_cos import CosS3Client
import sys
import logging
from base_config import *

filename = '1.log'
ssv = [ZJ_V2RAY]

new_headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36"
}

uuid = []


def getFreev2ray():
    req = requests.get(V2RAY_URL, headers=new_headers)
    soup = BeautifulSoup(req.text, 'lxml')
    uuid.append(soup.find_all('span', id='uuid')[0].text)
    uuid.append(soup.button.attrs['data-clipboard-text'])


def uptenxy(text):
    logging.basicConfig(level=logging.INFO, stream=sys.stdout)
    config = CosConfig(Region=COS_REGION, SecretId=COS_SECRETID,
                       SecretKey=COS_SECRETKEY, Token=TOKEN, Scheme=SCHEME)
    client = CosS3Client(config)
    response = client.put_object(
        Bucket=COS_BUCKET, Body=text, Key=COS_KEY,  EnableMD5=False)
    print('上传成功')


def update():
    ssv.append(uuid[1])
    text = ''
    for vm in ssv:
        text = text+vm+'\n'
    text = base64.b64encode(text.encode("utf8")).decode("ascii")
    uptenxy(text)
    with open(filename, "w+") as f:
        f.write('{}{}\n'.format(uuid[0], time.strftime(
            "%Y-%m-%d %H:%M:%S", time.localtime())))


if __name__ == "__main__":
    getFreev2ray()
    if not os.path.exists(filename) or os.path.getsize(filename) == 0:
        with open(filename, "w+") as f:
            f.write('{}\n{}'.format('7f2b6-a46b-44a',
                                    time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())))
    with open(filename, "r") as f:
        ss = f.readlines()
    if ss[0] != uuid[0]:
        update()
    else:
        with open(filename, 'a') as f:
            f.write('{}没有更新\n'.format(time.strftime(
                '%Y-%m-%d %H:%M:%S', time.localtime())))
        exit
